package org.sxp.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.sxp.common.annotation.DataFilter;
import org.sxp.common.utils.AeyePageResult;
import org.sxp.common.utils.PageInfo;
import org.sxp.common.utils.PageUtils;
import org.sxp.common.utils.Query;
import org.sxp.modules.sys.dao.SysDeptDao;
import org.sxp.modules.sys.entity.SysDeptEntity;
import org.sxp.modules.sys.service.SysDeptService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {
	
	@Override
	public List<SysDeptEntity> queryList(Map<String, Object> params){
		return baseMapper.queryList(params);
	}

	@Override
	public AeyePageResult queryListPage(Map<String, Object> params, PageInfo pageInfo) throws Exception{
		IPage page = baseMapper.queryListPage(
				new Query<SysDeptEntity>().getPage(pageInfo),
				params);
		return PageUtils.pageConvert(page);
	}

	@Override
	public List<String> queryDetpIdList(String parentId) {
		return baseMapper.queryDetpIdList(parentId);
	}

	@DataFilter(enable = false)
	@Override
	public List<String> getSubDeptIdList(String deptId){
		//部门及子部门ID列表
		List<String> deptIdList = new ArrayList<>();

		//获取子部门ID
		List<String> subIdList = queryDetpIdList(deptId);
		getDeptTreeList(subIdList, deptIdList);

		return deptIdList;
	}

	/**
	 * 递归
	 */
	private void getDeptTreeList(List<String> subIdList, List<String> deptIdList){
		for(String deptId : subIdList){
			List<String> list = queryDetpIdList(deptId);
			if(list.size() > 0){
				getDeptTreeList(list, deptIdList);
			}

			deptIdList.add(deptId);
		}
	}
}
